package com.blt.car.me;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.blt.car.R;
import com.blt.car.base.BaseTitleBarActivity;
import com.blt.car.base.ViewHolder;
import com.blt.car.me.adapter.BillAdapter;
import com.blt.car.utils.ActivityHelper;
import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;

import java.util.List;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.invoce.InvoiceApi;
import blt.app_core.api.invoce.InvoiceVo;
import blt.app_core.api.journey.JourneyApi;

/**
 * Created by xgp on 2017/6/20.
 */

public class BillManagerActivity extends BaseTitleBarActivity implements AdapterView.OnItemClickListener {
    private ListView billView;
    private BillAdapter billAdapter;
    private  int page = 1;
    private   int size = 15;
    private PtrClassicFrameLayout frameLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bill_manager;
    }

    @Override
    protected void initData() {
        super.initData();
        CoreManager.getCore(InvoiceApi.class).getInvoiceInfo(page, size);
    }

    @Override
    protected void initView(ViewHolder holder) {
        mTitleBar.setTitle("发票管理");
        mTitleBar.setRightText("开票说明");
        mTitleBar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        billView = holder.get(R.id.bill_manager_listview);
        billAdapter = new BillAdapter(this, R.layout.item_bill_manager);
        billView.setAdapter(billAdapter);

        billView.setOnItemClickListener(this);
        frameLayout = holder.get(R.id.bill_manager_frame);
        frameLayout.setLoadMoreEnable(true);
        frameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                CoreManager.getCore(InvoiceApi.class).getInvoiceInfo(page, size);
            }
        });
        frameLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                page++;
                CoreManager.getCore(InvoiceApi.class).getInvoiceInfo(page, size);
            }
        });
    }


    @CoreEvent(coreClientClass = InvoiceApi.class)
    public void onInvoiceList(List<InvoiceVo> data) {
        if (page == 1) {
            billAdapter.clear();
            frameLayout.refreshComplete();
        } else {
            frameLayout.loadMoreComplete(true);
        }
        billAdapter.addData(data);
    }


    @Override
    public void onItemClick(AdapterView<?> adapterVew, View view, int i, long l) {
        InvoiceVo vo = (InvoiceVo) billAdapter.getItem(i);
//        ActivityHelper.toBillDetail(this);
        openActivity(BillDetailActivity.class, BillDetailActivity.BILL_ID, vo.id);
    }

    //申请发票
    public void applyBill(View view) {
        ActivityHelper.toChooseTrips(this);
    }

    @CoreEvent(coreClientClass = InvoiceApi.class)
    public void onApplySuccess() {
        CoreManager.getCore(InvoiceApi.class).getInvoiceInfo(page, size);
    }

}
